package com.wonder.admin.tools;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wonder.admin.config.GenProperties;
import com.wonder.admin.domain.I118;
import com.wonder.admin.service.I118Service;
import com.wonder.common.common.AjaxMap;
import com.wonder.tools.model.ModuleInfo;


import lombok.extern.java.Log;

@Log
@Service
public class LanguageGenService {


	@Autowired
	private GenProperties genProperties;
	
    @Autowired
    I118Service i118Service;
    
	public  Map<String, Object> getLanguageJson(String language) {
		
		List<I118> list=i118Service.list(new QueryWrapper<I118>().eq("type", 1));
		List<String> controllers=new ArrayList<String>();
		
		String contents="";	
		for (I118 i118 : list) {
			String controller=i118.getController();
			if(controllers.contains(controller)) continue;
			controllers.add(controller);
	
		}
		
		
		for (String item : controllers) {
			String itemPart="\t"+item+":{\n";
			for (I118 i118 : list) {
				if(i118.getController().equals(item)) {
					Object lanObject=getFieldValueByName("language_" +language, i118);
					if(lanObject!=null) {
						String lanValue=lanObject.toString();
						itemPart +="\t"+"\t"+i118.getMessage_id().substring(i118.getMessage_id().indexOf('.')+1) +":\""+lanValue+ "\","+"\n";
					}
				}
			}
			itemPart=itemPart+"},\n";
			
			contents=contents +itemPart;
			
			
		}
		
		return createLanguage("module.exports= {\n"+contents+"\n}",language);
		
	}
	
	
public  Map<String, Object> getLanguageJsonUniApp(String language) {
		
		List<I118> list=i118Service.list(new QueryWrapper<I118>().eq("type", 1).orderByAsc("controller","message_id"));
		
		String contents="{";	
		
		boolean first=true;
		for (I118 item : list) {
			Object lanObject=getFieldValueByName("language_" +language, item);
			if(lanObject!=null) {
				String lanValue=lanObject.toString();
				if(first) {
					contents +="\t\""+item.getMessage_id() +"\":\""+lanValue+ "\"";
					first=false;
				}else {
					contents +=",\n"+"\t\""+item.getMessage_id() +"\":\""+lanValue+ "\"";
				}
			}
			
		  }

			
        contents=contents+"\n}";
		
		return createLanguageUniApp(contents,language);
		
	}


	/**
	 * Create uni app Entity
	 * @return
	 */
	public  Map<String, Object> createLanguageUniApp(String contents,String language) {
		ModuleInfo dataModel=new ModuleInfo();	
	   dataModel.setProject(contents);
	 
		String fileUrl=genProperties.uniAppProjectDir+"/locale/"+language.replace("_", "-")+".json";
		try {
	    	File file = new File(fileUrl);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
		    FileWriter writer = new FileWriter(fileUrl);
		    writer.write(contents);
		    writer.flush();
		    writer.close();
		    } catch (IOException e) {
		      e.printStackTrace();
		      return AjaxMap.failed("创建"+fileUrl+"失败");
		    }
	   return AjaxMap.success("创建"+fileUrl+"成功");
	}
	
	
	/**
	 * Create Entity
	 */
	public  Map<String, Object> createLanguage(String contents,String language) {
		ModuleInfo dataModel=new ModuleInfo();	
       dataModel.setProject(contents);
		
		String fileUrl=genProperties.vueProjectDir+"/src/assets/language/"+language+".js";
		try {
	    	File file = new File(fileUrl);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
		    FileWriter writer = new FileWriter(fileUrl);
		    writer.write(contents);
		    writer.flush();
		    writer.close();
		    } catch (IOException e) {
		      e.printStackTrace();
		      return AjaxMap.failed("创建"+fileUrl+"失败");
		    }
	   return AjaxMap.success("创建"+fileUrl+"成功");
	}
	
	/**
	 * 根据属性名获取属性值
	 * @param fieldName 属性名称
	 * @param o 对象
	 * @return
	 */
	private static Object getFieldValueByName(String fieldName, Object o) {
		try {
			String firstLetter = fieldName.substring(0, 1).toUpperCase();
			String getter = "get" + firstLetter + fieldName.substring(1);
			Method method = o.getClass().getMethod(getter, new Class[] {});
			Object value = method.invoke(o, new Object[] {});
			return value;
		} catch (Exception e) {
			log.warning("根据属性名获取属性值异常："+e.getMessage());
			return null;
		}
	}


	
	

}
